package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.domain.Question;
import com.tanhua.dubbo.mappers.QuestionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 陌生人问题服务提供者
 */
@DubboService
public class QuestionApiImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionApi {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void saveQuestion(String content, Long userId) {
        //判断当前用户是否已设置问题
        //LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        //wrapper.eq(Question::getUserId,userId);

        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);


        Question questionApiOne = questionMapper.selectOne(wrapper);

        //创建陌生人问题对象
        Question question = new Question();
        //为问题对象设置信息
        question.setUserId(userId);
        question.setTxt(content);
        if(questionApiOne == null){
            //保存
            questionMapper.insert(question);
        }else{
            //更新用户设置的问题
            questionMapper.update(question,wrapper);
        }
    }

    /**
     * 通过userid查询question对象
     * @param userId
     * @return
     */
    @Override
    public Question getByUserId(Long userId) {
       LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper();
       wrapper.eq(Question::getUserId,userId);
       return questionMapper.selectOne(wrapper);
    }
}
